package msr.frontend;

import java.util.HashMap;
import java.util.Map;

import msr.service.UserService;

import org.apache.commons.codec.digest.DigestUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ChangepasswdAction extends ActionSupport {
	UserService uService = new UserService();
	private String currentpasswd;
	private String newpasswd;
	private String checkpasswd;
	Map<String, Object> session = ActionContext.getContext().getSession();
	Map<String, String> rtData;
	private String username;
	private String passwd;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public String execute() {
		this.setUsername((String) session.get("username"));
		this.setPasswd((String) session.get("passwd"));
		boolean check = false;
		if (currentpasswd == null && newpasswd == null && checkpasswd == null)
			return SUCCESS;
		else {
			rtData = this.checkPasswd(currentpasswd, newpasswd, checkpasswd);
			if (rtData.get("error").equals("true")) {
				if (rtData.get("type").equals("notvalid"))
					addActionError(getText("error.notvalid"));
				else if (rtData.get("type").equals("conflict"))
					addActionError(getText("error.conflict"));
				else if (rtData.get("type").equals("notmatch"))
					addActionError(getText("error.notmatch"));
				else
					addActionError(getText("error.undefine"));
				return ERROR;
			} else {
				check = uService.changePasswd(newpasswd);
				if (check) {
					// update session
					String passMd5 = DigestUtils.md5Hex(newpasswd);
					session.put("passwd", passMd5);
					addActionMessage("message.changepasswdsuccess");
					return SUCCESS;
				} else
					return ERROR;
			}
		}
	}

	public Map<String, String> checkPasswd(String currPasswd, String newPasswd,
			String checkPasswd) {
		Map<String, String> rtData = new HashMap<String, String>();
		// need check current passwd with session variable
		String passwdMd5 = DigestUtils.md5Hex(currPasswd);
		String sessionPasswd = (String) session.get("passwd");
		System.out.println("value of sessionpasswd " + sessionPasswd);
		System.out.println("curr passwd " + passwdMd5);
		if (!passwdMd5.equals(sessionPasswd)) {
			rtData.put("type", "notvalid");
			rtData.put("error", "true");
		} else if (currPasswd.equals(newPasswd)) {
			rtData.put("type", "conflict");
			rtData.put("error", "true");
		} else if (!newPasswd.equals(checkPasswd)) {
			rtData.put("type", "notmatch");
			rtData.put("error", "true");
		} else
			rtData.put("error", "false");
		return rtData;
	}

	public String getCurrentpasswd() {
		return currentpasswd;
	}

	public void setCurrentpasswd(String currentpasswd) {
		this.currentpasswd = currentpasswd;
	}

	public String getNewpasswd() {
		return newpasswd;
	}

	public void setNewpasswd(String newpasswd) {
		this.newpasswd = newpasswd;
	}

	public String getCheckpasswd() {
		return checkpasswd;
	}

	public void setCheckpasswd(String checkpasswd) {
		this.checkpasswd = checkpasswd;
	}

	public Map<String, String> getRtData() {
		return rtData;
	}

	public void setRtData(Map<String, String> rtData) {
		this.rtData = rtData;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

}
